package com.webware.one.client.popup;

import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class AbstractPopupWindow extends DialogBox {

	AbstractButtonsPanel buttonPanel;
	VerticalPanel layout = new VerticalPanel();
	Widget widget = null;

	public AbstractPopupWindow(String title, Widget widget, EButton... buttons) {
		this(title, widget, true, buttons);
	}

	public AbstractPopupWindow(String title, Widget widget, final boolean closeOnButton, EButton... buttonsDescription) {
		super();
		setText(title);
		setModal(true);
		setGlassEnabled(true);
		setAnimationEnabled(true);

		layout.setSpacing(0);
		layout.setWidth("100%");
		layout.setHeight("100%");
		layout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		if (widget != null) {
			this.widget = widget;
			layout.add(widget);
		}

		buttonPanel = new AbstractButtonsPanel(buttonsDescription) {
			@Override
			public void onButtonClick(EButton choice) {
				this.setButtonsEnabled(false);
				onFinish(choice);
				if (closeOnButton)
					AbstractPopupWindow.this.hide();
			}
		};
		layout.add(buttonPanel);

		this.setWidget(layout);
	}

	public AbstractButtonsPanel getButtonPanel() {
		return buttonPanel;
	}

	public void setButtonPanel(AbstractButtonsPanel buttonPanel) {
		this.buttonPanel = buttonPanel;
	}

	public void setEnabled(boolean enabled) {
		buttonPanel.setButtonsEnabled(enabled);
	}

	public void setFocuse(EButton choice) {
		buttonPanel.setFocuse(choice);
	}

	@Override
  public Widget getWidget() {
		return widget;
	}

	/** Нажали на одну из кнопок */
	public abstract void onFinish(EButton choice);
}
